package com.zhisuo.apicenter.service.imp;


import com.zhisuo.apicenter.model.*;
import com.zhisuo.apicenter.service.UserManService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserManServiceImp extends BaseServiceImp implements UserManService {


    @Override
    public List<DeliveryOrder> getListDeliveryOrderByReceiptMobile(String mobile,String status,Integer page,Integer pagesize) {


        //0待预约  1待取件  3已完成
        String sql="";
        if(status.equals("0")){
            sql= "select * from box_delivery_order where receiptusermobile=? and status in(0) and ordertype=0 order by create_time desc limit ?,?";
        }else if(status.equals("1")||status.equals("2")){
            sql= "select * from box_delivery_order where receiptusermobile=? and status in(1,2,4)  order by create_time desc limit ?,?";
        }else{
            sql= "select * from box_delivery_order where receiptusermobile=? and status in(3)  order by create_time desc limit ?,?";
        }
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{mobile, (page - 1) * pagesize, pagesize});
    }

    @Override
    public DeliveryOrder getDeliveryOrderById(String orderid) {
        String sql="Select * from box_delivery_order where id=?";
        return getEntry(sql,new DeliveryOrderRowMapper(),new Object[]{orderid});
    }

    @Override
    public void updateReceiptOrderSysStatusById(String orderid,String receiptcode,String addresscode,int addressid) {
        String sql="update box_delivery_order set status=?,receiptuser_confirmtime=?,receiptcode=?,receiptuseraddresscode=?,receiptuseraddressid=?,isovertime=0 where id=?";
        update(sql,new Object[]{1,new Date(),receiptcode,addresscode,addressid,orderid});
    }


    @Override
    public void updateReceiptOrderSysTimeStatusById(String orderid,String receiptcode,String addresscode,int addressid, String receiptuserorderstarttime, String receiptuserorderendtime) {
        String sql="update box_delivery_order set status=?,receiptuser_confirmtime=?,receiptcode=?,receiptuseraddresscode=?,receiptuseraddressid=?,isovertime=0,receiptuserorderstarttime=?,receiptuserorderendtime=? where id=?";
        update(sql,new Object[]{2,new Date(),receiptcode,addresscode,addressid,receiptuserorderstarttime,receiptuserorderendtime,orderid});
    }

    @Override
    public List<ZhiSuoUserAddress> getListUserAddressByUserId(String userid) {
        String sql="select * from zhisuo_user_address where userid=? and status=1";
        return getEntryList(sql,new ZhiSuoUserAddressRowMapper(),new Object[]{userid});
    }

    @Override
    public ParkAreaBuild getParkAreaBuildById(Integer parkareabuildid) {
        String sql="Select * from parkarea_build_info where id=?";
        return getEntry(sql,new ParkAreaBuildRowMapper(),new Object[]{parkareabuildid});
    }

    @Override
    public ParkArea getParkAreaById(int parkareaid) {
        String sql="Select * from parkarea_info where id=?";
        return getEntry(sql,new ParkAreaRowMapper(),new Object[]{parkareaid});
    }

    @Override
    public void updateReceiptOrderTimeStatusById(String orderid, String addressid, String receiptuserorderstarttime, String receiptuserorderendtime,String receiptcode,String addressCode) {
        String sql="update box_delivery_order set isovertime=0,status=?,receiptuseraddressid=?,receiptuser_confirmtime=?,receiptcode=?,receiptuserorderstarttime=?,receiptuserorderendtime=?,receiptuseraddresscode=? where id=? " ;
        update(sql,new Object[]{2,addressid,new Date(),receiptcode,receiptuserorderstarttime,receiptuserorderendtime,addressCode,orderid});
    }

    @Override
    public String getMobilByUserId(String userid) {
        String sql="select mobile from zhisuo_user where id=?";

        return queryString(sql,new Object[]{userid});
    }

    @Override
    public List<DeliveryOrder> getOrderIdByCarIdAndMobile(String carid, String mobile) {
        String sql="select * from box_delivery_order where receiptusermobile =? and carid=? and status in(1,2,4)";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{mobile,carid});
    }

    @Override
    public String getOrderIdByCarIdAndReceiptcode(String carid, String receiptcode) {
        String sql="select id from box_delivery_order where carid=? and receiptcode=? ";
        return queryString(sql,new Object[]{carid,receiptcode});
    }

    @Override
    public void updateOrderByReceiptcode(String orderid,String userid) {
        String sql="update box_delivery_order set status=?,receipt_time=?,receiptcodeuserid=? where id=? " ;
        update(sql,new Object[]{3,new Date(),userid,orderid});
    }

    @Override
    public List<Integer> getDeliveryOrderCountByOrderTime(String receiptuserorderstarttime, String receiptuserorderendtime) {
        String sql="select count(1) from box_delivery_order where receiptuserorderstarttime=? and receiptuserorderendtime=? group by receiptusermobile";

        return queryIntegerArray(sql,new Object[]{receiptuserorderstarttime,receiptuserorderendtime});
    }

    @Override
    public List<DeliveryOrder> getListDeliveryOrderBySync(String carid) {
        String sql="select * from box_delivery_order where ((status =1 and isovertime=0 ) or (status=2 and  receiptuserorderstarttime >= now() and receiptuserorderstarttime<=date_add(now(), interval 63 minute))) and  carid=?";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{carid});
    }

    @Override
    public ZhiSuoUserAddress getZhiSuoUserAddressById(int receiptuseraddressid) {
        String sql="select * from zhisuo_user_address where id=?";
        return getEntry(sql,new ZhiSuoUserAddressRowMapper(),new Object[]{receiptuseraddressid});
    }

    @Override
    public List<String> getListCarIdDeliveryOrder() {
        String sql="select carid from box_delivery_order where (status =1 and isovertime=0 ) or (status=2 and  receiptuserorderstarttime >= now() and receiptuserorderstarttime<=date_add(now(), interval 63 minute)) group by carid";
        return queryStringArray(sql,new Object[]{});
    }

    @Override
    public void updateZhiSuoCarNextStopListStauts(String carid) {
        String sql="update zhisuo_car_nextstoplist set status=1,update_time=?  where carid=?";
        update(sql,new Object[]{new Date(),carid});
    }

    @Override
    public void insertNewZhiSuoCarNextStopList(String carid, String nextstop) {
        System.out.println("sbsbsbsb-----sbsbsbsb");
        String sql="insert into zhisuo_car_nextstoplist(carid,nextstop,status,create_time) values(?,?,?,?)";
        update(sql,new Object[]{carid,nextstop,0,new Date()});

    }

    @Override
    public ZhiSuoCar getZhiSuoCarByCarId(String carid) {
        String sql="select * from zhisuo_car where id=?";
        return getEntry(sql,new ZhiSuoCarRowMapper(),new Object[]{carid});
    }

    @Override
    public void insertNewZhiSuoCarNextStopTcp(String sn, int carid, String name, String nextstop,String stop_timeout_sec,String beforestop) {
        String sql="delete from zhisuo_car_nextstoptcp where carid=?";
        update(sql,new Object[]{carid});
        System.out.println("insertNewZhiSuoCarNextStopTcp--------"+carid+"---"+name+"----"+nextstop);
        sql="insert into zhisuo_car_nextstoptcp(carid,vin,name,stop,start_time,status,beforestop) values(?,?,?,?,date_add(now(), interval ? minute),?,?)";
        update(sql,new Object[]{carid,sn,name,nextstop,stop_timeout_sec,0,beforestop});
    }

    @Override
    public String getNextStopByCarId(int carid) {
        String sql="select nextstop from zhisuo_car_nextstoplist where carid=? and status=0 order by id asc";
        return queryString(sql,new Object[]{carid});
    }

    @Override
    public ZhiSuoUser getZhiSuoUserByMobile(String receiptusermobile) {
        String sql="select * from zhisuo_user where mobile=?";
        return getEntry(sql,new ZhiSuoUserRowMapper(),new Object[]{receiptusermobile});
    }

    @Override
    public ZhiSuoUserAddress getZhisuoFlagUserAddressByUserId(int id) {
        String sql="select * from zhisuo_user_address where userid=? and flag=1";
        return getEntry(sql,new ZhiSuoUserAddressRowMapper(),new Object[]{id});
    }

    @Override
    public Integer getOrderStatusByOrderId(String orderid) {
        String sql="select status from box_delivery_order where id=?";
        return queryInteger(sql,new Object[]{orderid});
    }

    @Override
    public void updateNextStopListStatusByCarId(String carid) {
        String sql="update zhisuo_car_nextstoplist set status=1,update_time=? where carid=?";
        update(sql,new Object[]{new Date(),carid});
    }

    @Override
    public void insertNewZhiSuoCarNextStopSms(String receiptcode, String receiptusermobile,String carid,Integer smstype) {
//        String sql="delete from zhisuo_car_nextstopsms where carid=?";
//        update(sql,new Object[]{carid});
        String  sql="insert into zhisuo_car_nextstopsms(carid,phoneNumbers,smsparams,start_time,status,smstype) values(?,?,?,date_add(now(), interval 1 second),?,?)";
        update(sql,new Object[]{carid,receiptusermobile,receiptcode,0,smstype});
    }

    @Override
    public String getNextStopTcpIdByCarIdAndStop(int carid, String homestation) {
//        String sql="select id from zhisuo_car_nextstoptcp where carid=? and stop=? and status=0";
        String sql="select id from zhisuo_car_nextstoptcp where carid=? and stop=?";
        return queryString(sql,new Object[]{carid,homestation});

    }

    @Override
    public String getSmsStatusByCaridMobile(String receiptcode, String receiptusermobile, String carid) {
        String sql="select status from zhisuo_car_nextstopsms where carid=? and phoneNumbers=? and smsparams=?";
        return queryString(sql,new Object[]{carid,receiptusermobile,receiptcode});
    }

    @Override
    public List<String> getSendZhiSuoCarByAreaId(int parkareaid) {
        String sql="select carid from parkarea_car where parkareaid=?";
        return queryStringArray(sql,new Object[]{parkareaid});
    }

    @Override
    public String insertNewUserSendOrder(String userid, String carid, String boxnum, int boxid, String boxcode, String sendmobile, String senduseraddressid, String senduseraddresscode, String senduserorderstarttime, String senduserorderendtime, String sendcode) {
        String sql="insert into box_usersend_order(userid,carid,boxnum,boxid,boxcode,sendmobile,senduseraddresscode,create_time,status,senduseraddressid,senduserorderstarttime,senduserorderendtime,sendcode) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return saveAndGetStringId(sql,new Object[]{userid,carid,boxnum,boxid,boxcode,sendmobile,senduseraddresscode,new Date(),0,senduseraddressid,senduserorderstarttime,senduserorderendtime,sendcode});
    }

    @Override
    public void insertNewSendOrderList(String orderid, String code) {
        String sql="insert into box_usersend_order_codelist(orderid,paymentcode) values(?,?)";
        update(sql,new Object[]{orderid,code});
    }

    @Override
    public String getOrderIdByCarIdAndSendcode(String carid, String sendcode) {
        String sql="select id from box_usersend_order where carid=? and sendcode=? and status=1 ";
        return queryString(sql,new Object[]{carid,sendcode});
    }

    @Override
    public SendOrder getSendOrderIdByCarIdAndMobile(String carid, String mobile) {
        String sql="select * from box_usersend_order where sendmobile =? and carid=? and status=1";
        return getEntry(sql,new SendOrderRowMapper(),new Object[]{mobile,carid});
    }

    @Override
    public void updateOrderBySendcode(String orderid, String userid) {
        String sql="update box_usersend_order set status=?,send_time=?,sendcodeuserid=? where id=? " ;
        update(sql,new Object[]{2,new Date(),userid,orderid});
    }

    @Override
    public void insertNewZhiSuoCarSendNextStopTcp(String sn, int carid, String name, String nextstop,String stop) {
        String sql="delete from zhisuo_car_nextstoptcp where carid=?";
        update(sql,new Object[]{carid});
        System.out.println("secondStopTcp--------"+carid+"---"+name+"----"+nextstop);
        sql="insert into zhisuo_car_nextstoptcp(carid,vin,name,stop,start_time,status,beforestop) values(?,?,?,?,date_add(now(), interval 1 SECOND),?,?)";
        update(sql,new Object[]{carid,sn,name,nextstop,0,stop});
    }

    @Override
    public void insertNewZhiSuoCarSendNextStopSms(String receiptcode, String receiptusermobile,String carid,int smstype) {
        String  sql="insert into zhisuo_car_nextstopsms(carid,phoneNumbers,smsparams,start_time,status,smstype) values(?,?,?,date_add(now(), interval 1 SECOND),?,?)";
        update(sql,new Object[]{carid,receiptusermobile,receiptcode,0,smstype});

    }

    @Override
    public List<DeliveryOrder> getListNoDeliveryOrderBySync(String carid,String beforestop) {
//        String sql="select * from box_delivery_order where (status=4 and ordertype=1) or(status=2 and  now()>=receiptuserorderendtime and ordertype=0) and  carid=?";
        String sql="select * from box_delivery_order where ((status=4 and ordertype=1 ) or((status =1 or (status=2 and now()>=receiptuserorderendtime))and ordertype=0)) and  carid=? and receiptuseraddresscode=?";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{carid,beforestop});
    }

    @Override
    public void updateOrderStatusAgainOrderByOrderId(int orderid) {
        String sql="update box_delivery_order set status=?,receiptuserorderstarttime=?,receiptuserorderendtime=?,isovertime=1 where id=? " ;
        update(sql,new Object[]{0,null,null,orderid});
    }

    @Override
    public String getBeforeStopByCarId(int carid) {
        String sql="select beforestop from zhisuo_car_beforestop where carid=? and status=0";
        return queryString(sql,new Object[]{carid});
    }

    @Override
    public void insertBeforeStopByCarId(int carid, String stop) {
        String sql="delete from zhisuo_car_beforestop where carid=?";
        update(sql,new Object[]{carid});
        sql="insert into zhisuo_car_beforestop(carid,beforestop,status) values(?,?,?)";
        update(sql,new Object[]{carid,stop,0});
    }

    @Override
    public void updateUserIntegralByUserId(String userid, Integer award_integral) {
        String sql="update zhisuo_user set integral=integral+? where id=? " ;
        update(sql,new Object[]{award_integral,userid});
    }

    @Override
    public void insertNewZhiSuoUserIntegralRecord(String userid, int orderid, Integer award_integral, int type, int sourcetype) {
        String sql="select integral from zhisuo_user where id=?";
        Integer afterintegralnum=queryInteger(sql,new Object[]{userid});
        //sourcetype 0收快件    1寄快件   2签到  3提现
        String content="";
        if(sourcetype==0){
            content="收快件";
        }else if(sourcetype==1){
            content="寄快件";

        }else if(sourcetype==2){
            content="签到";

        }else if(sourcetype==3){
            content="提现";

        }else{
            content="预留";
        }
        sql="insert into zhisuo_user_integral_record(orderid,integralnum,afterintegralnum,createtime,userid,type,status,content,sourcetype) values(?,?,?,?,?,?,?,?,?)";
        update(sql,new Object[]{orderid,award_integral,afterintegralnum,new Date(),userid,type,1,content,sourcetype});
    }

    @Override
    public void insertZhiSuoUserSignRecord(String userid, String daydate) {
        String sql="insert into zhisuo_user_signrecord(userid,daydate,create_time) values(?,?,?)";
        update(sql,new Object[]{userid,daydate,new Date()});
    }

    @Override
    public List<SendOrder> getListSendOrderByReceiptMobile(String userid, String status, Integer page, Integer pagesize) {

        //0待寄件  1收件中  2
        String sql="";
        if(status.equals("0")){
            sql= "select * from box_usersend_order where userid=? and status=0  order by create_time desc limit ?,?";
        }else if(status.equals("1")){
            sql= "select * from box_usersend_order where userid=? and status in(1,2) order by create_time desc limit ?,?";
        }else  if(status.equals("2")){
            sql= "select * from box_usersend_order where userid=? and status=3  order by create_time desc limit ?,?";
        }else{

        }
        return getEntryList(sql,new SendOrderRowMapper(),new Object[]{userid, (page - 1) * pagesize, pagesize});


    }

    @Override
    public SendOrder getSendOrderById(String orderid) {
        String sql="Select * from box_usersend_order where id=?";
        return getEntry(sql,new SendOrderRowMapper(),new Object[]{orderid});
    }

    @Override
    public Integer getHasOrderNumByUserId(String usermobile,Integer type) {
        String sql="";
        if(type==0){
            //快递待取件
            sql="select count(1) from box_delivery_order where receiptusermobile=? and status in (1,2,4) and ordertype=0";

        }

        if(type==1){
            //零售待取件
           sql="select count(1) from box_delivery_order where receiptusermobile=? and status in(1,4) and ordertype=1 ";

        }

        if(type==2){
            //快递待预约
            sql="select count(1) from box_delivery_order where receiptusermobile=? and status=0 and ordertype=0 ";

        }


        if(type==3){
            //零售待预约
            sql="select count(1) from box_delivery_order where receiptusermobile=? and status=0 and ordertype=1 ";

        }
        return queryInteger(sql,new Object[]{usermobile});

    }

    @Override
    public List<SendOrder> getUserSendOrderIdByCarIdStatus(String carid) {
        String sql="select * from box_usersend_order where carid=? and status=2";
        return getEntryList(sql,new SendOrderRowMapper(),new Object[]{carid});
    }

    @Override
    public void updateSendOrderByReceiptcode(String orderid, String userid) {
        String sql="update box_usersend_order set status=?,receipt_time=?,receiptcodeuserid=? where id=? " ;
        update(sql,new Object[]{3,new Date(),userid,orderid});
    }

    @Override
    public List<String> getListHasBoxByCarId(int carid,String stop) {
        String sql="select id from box_delivery_order where (status =4 or status =1 or (status=2 and now() >=  receiptuserorderstarttime and now()<= receiptuserorderendtime)) and carid=? and receiptuseraddresscode=?";
        return queryStringArray(sql,new Object[]{carid,stop});
    }

    @Override
    public void insertNewZhiSuoCarNextStopTcpSecond(String sn, int carid, String name, String nextstop) {
        String sql="delete from zhisuo_car_nextstoptcp where carid=?";
        update(sql,new Object[]{carid});
        sql="insert into zhisuo_car_nextstoptcp(carid,vin,name,stop,start_time,status) values(?,?,?,?,date_add(now(), interval 1 second),?)";
        update(sql,new Object[]{carid,sn,name,nextstop,0});
    }

    @Override
    public String getParkAreaIdByCarId(Integer carid) {
        String sql="select parkareaid from parkarea_car where carid=?";
        return queryString(sql,new Object[]{carid});
    }

    @Override
    public ZhiSuoUser getZhiSuoUserById(String userid) {
        String sql="select * from zhisuo_user where id=?";
        return getEntry(sql,new ZhiSuoUserRowMapper(),new Object[]{userid});
    }

    @Override
    public List<DeliveryOrder> getListDeliveryNoOrderByReceiptMobile(String mobile) {
        String sql= "select * from box_delivery_order where receiptusermobile=? and status in(0) and ordertype=0";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{mobile});
    }

    @Override
    public Integer getHasSellerOrderNumByUserId(String userid, int type) {
        if(type==0){
            //待派送
            String sql="select count(1) from box_delivery_order where userid=? and status=0 and ordertype=1";
            return queryInteger(sql,new Object[]{userid});
        }else{
            //派送中
            String sql="select count(1) from box_delivery_order where userid=? and status=4 and ordertype=1";
            return queryInteger(sql,new Object[]{userid});
        }

    }

    @Override
    public List<String> getListCarIdSendOrder() {
        String sql="select carid from box_usersend_order where status =0 group by carid";
        return queryStringArray(sql,new Object[]{});
    }

    @Override
    public List<SendOrder> getListSendOrderBySync(String carid) {
        String sql="select * from box_usersend_order where status = 0 and  senduserorderstarttime >= now() and senduserorderendtime<=date_add(now(), interval 69 minute) and carid=?";
        return getEntryList(sql,new SendOrderRowMapper(),new Object[]{carid});
    }

    @Override
    public void updateSendOrderStatusByOrderId(int id) {
        String sql="update box_usersend_order set status=1 where id=?";
        update(sql,new Object[]{id});
    }

    @Override
    public void updateDeliveryOrderGiveOtherUserByStatus(int id, String giveusermobile,String receiptusermobile) {
        String sql="update box_delivery_order set giveusermobile=?,receiptusermobile=? where id=? ";
        update(sql,new Object[]{receiptusermobile,giveusermobile,id});
    }

    @Override
    public Integer getHasSendOrderNumByUserId(String mobile) {
        String  sql="select count(1) from box_usersend_order where sendmobile=? and status=0 ";

        return queryInteger(sql,new Object[]{mobile});
    }

    @Override
    public void updateSellerOrderStatusAgainOrderByOrderId(int orderid) {
        String sql="update box_delivery_order set status=?,receiptuserorderstarttime=?,receiptuserorderendtime=?,isovertime=1 where id=? " ;
        update(sql,new Object[]{0,null,null,orderid});
    }

    @Override
    public Integer getHasSellerSendOrderNumByUserId(String userid) {
        String sql="select count(1) from box_delivery_order where status in(0,1,2) and userid=?";
        return queryInteger(sql,new Object[]{userid});
    }

    @Override
    public ParkAreaBuild getParkAreaBuildByCode(String receiptuseraddresscode) {
        String sql="select * from parkarea_build_info where code=?";
        return getEntry(sql,new ParkAreaBuildRowMapper(),new Object[]{receiptuseraddresscode});
    }

    @Override
    public void insertFirstNewZhiSuoCarNextStopTcp(String sn, int carid, String name, String nextstop,String stop_timeout_sec,String beforestop) {
        String sql="delete from zhisuo_car_nextstoptcp where carid=?";
        update(sql,new Object[]{carid});
        sql="insert into zhisuo_car_nextstoptcp(carid,vin,name,stop,start_time,status,beforestop) values(?,?,?,?,date_add(now(), interval ? second),?,?)";
        update(sql,new Object[]{carid,sn,name,nextstop,1,0,beforestop});
    }

    @Override
    public void updateReceiptOrderSysTimeStatusByBuildId(String orderid,String receiptcode,String addresscode,int addressid, String receiptuserorderstarttime, String receiptuserorderendtime) {
        String sql="update box_delivery_order set status=?,receiptuser_confirmtime=?,receiptcode=?,receiptuseraddresscode=?,receiptuserbuildid=?,isovertime=0,receiptuserorderstarttime=?,receiptuserorderendtime=? where id=?";
        update(sql,new Object[]{2,new Date(),receiptcode,addresscode,addressid,receiptuserorderstarttime,receiptuserorderendtime,orderid});
    }

    @Override
    public String getParkAreaBuildIdByUserid(String userid) {
        String sql="select parkareabuildid from zhisuo_user_address where userid=? and status=1 and flag=1";
        return queryString(sql,new Object[]{userid});
    }

    @Override
    public String getTimeTableFromParkAreaId(int parkareaid) {
        String sql="select timetable from parkarea_info where id=? ";
        return queryString(sql,new Object[]{parkareaid});
    }

    @Override
    public List<String> getListCarIdRubbishOrder() {
        String sql="select carid from box_rubbish_order where status=0 and  receiptuserorderstarttime <= date_add(now(), interval 13 minute) group by carid";
        return queryStringArray(sql,new Object[]{});
    }

    @Override
    public List<RubbishOrder> getListRubbishOrderBySync(String carid) {
        String sql="select * from box_rubbish_order where status=0 and  receiptuserorderstarttime <= date_add(now(), interval 13 minute) and  carid=?";
        return getEntryList(sql,new RubbishOrderRowMapper(),new Object[]{carid});
    }
}
